package codeTop;

import java.util.PriorityQueue;
import java.util.Queue;

/**
 * 去95
 *
 * @author 雨凡
 * @version 去95.java, v0.1
 * @date 2025/07/05/13:35
 */
public class q295 {
    class MedianFinder {
        Queue<Integer> left;
        Queue<Integer> right;

        public MedianFinder() {
            left = new PriorityQueue<>((a,b)->b-a);
            right = new PriorityQueue<>((a,b)->a-b);
        }

        public void addNum(int num) {
            if (left.isEmpty() || left.peek() > num) {
                left.add(num);
            }else {
                right.add(num);
            }
            if (left.size() > right.size() + 1) {
                right.add(left.poll());
            }
            if (right.size() > left.size()) {
                left.add(right.poll());
            }
        }

        public double findMedian() {
            if (left.size() == right.size()) {
                return (double)(left.peek() + right.peek()) /2;
            }else {
                return left.peek();
            }
        }
    }
}
